#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

class Solution 
{
public:
    vector<vector<int>> threeSum(vector<int>& nums) 
    {
        sort(nums.begin(), nums.end());
        vector<vector<int>> results;
        if(nums.empty() == true  || nums[0] > 0)
        {
            return results;
        }
        int length = nums.size();
        for(int i = 0; i < length - 2; i++)
        {
            // 跳过重复的第一个元素
            if(i > 0 && nums[i] == nums[i - 1])
            {
                continue;
            }
            int left = i + 1;
            int right = length - 1;
            while(left < right)
            {
                int sum = nums[i] + nums[left] + nums[right];
                if(sum < 0)
                {
                    left ++;
                    continue;
                }
                else if(sum > 0)
                {
                    right--;
                    continue;
                }
                else
                {
                    vector<int> result = {nums[i], nums[left], nums[right]};
                    results.push_back(result);
                    left++;
                    right--;
                    while(left < right && nums[left] == nums[left - 1])
                    {
                        left ++;
                    }
                    while (left < right && nums[right] == nums[right + 1])
                    {
                        right--;
                    }
                }
            }
        }
        return results;
    }
};

int main()
{
    Solution solution;

    // 测试案例1: 标准三数之和案例
    vector<int> nums1 = {-1, 0, 1, 2, -1, -4};
    vector<vector<int>> result1 = solution.threeSum(nums1);
    cout << "测试案例1: [-1, 0, 1, 2, -1, -4]" << endl;
    cout << "结果:" << endl;
    for(const auto& triplet : result1) {
        cout << "[" << triplet[0] << ", " << triplet[1] << ", " << triplet[2] << "]" << endl;
    }
    cout << endl;

    // 测试案例2: 全部为0
    vector<int> nums2 = {0, 0, 0, 0};
    vector<vector<int>> result2 = solution.threeSum(nums2);
    cout << "测试案例2: [0, 0, 0, 0]" << endl;
    cout << "结果:" << endl;
    for(const auto& triplet : result2) {
        cout << "[" << triplet[0] << ", " << triplet[1] << ", " << triplet[2] << "]" << endl;
    }
    cout << endl;

    // 测试案例3: 无解案例
    vector<int> nums3 = {1, 2, 3, 4, 5};
    vector<vector<int>> result3 = solution.threeSum(nums3);
    cout << "测试案例3: [1, 2, 3, 4, 5]" << endl;
    cout << "结果:" << endl;
    for(const auto& triplet : result3) {
        cout << "[" << triplet[0] << ", " << triplet[1] << ", " << triplet[2] << "]" << endl;
    }
    cout << endl;

    // 测试案例4: 有重复数字
    vector<int> nums4 = {-2, 0, 0, 2, 2};
    vector<vector<int>> result4 = solution.threeSum(nums4);
    cout << "测试案例4: [-2, 0, 0, 2, 2]" << endl;
    cout << "结果:" << endl;
    for(const auto& triplet : result4) {
        cout << "[" << triplet[0] << ", " << triplet[1] << ", " << triplet[2] << "]" << endl;
    }
    cout << endl;

    return 0;
}